<template>

  <div>

    <el-form  :model="form" label-width="80px">
      <el-form-item label="ID">
        <el-input v-model="form.itemId" readonly></el-input>
      </el-form-item>
      <el-form-item label="类型">

        <el-select  v-model="form.type"  placeholder="请选择" @change="changeType">
          <el-option
            v-for="item in typeList"
            :key="item.type"
            :label="item.caption"
            :value="item.type">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="代码">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="form.cation"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save()">保存</el-button>

      </el-form-item>
    </el-form>



  </div>

</template>

<script>
import {getForm,save,update,getTypeList} from "@/api/common/constatnts_item";
import {getList} from "@/api/common/constatnts_type";
export default{
  name:'DeptEdid',
  props:["id"],
  data(){
    return {
      type:{},
      form:{
        itemId:'',
        type:'',
        typeName:'',
        code:'',
        cation:''
      },
      typeList:[]
    }
  },
  created() {
    if(this.id){
      this.getForm();
    }

    this.fetchTypeList();

  },
  methods:{
    changeType(type){
      console.log(type);

      this.typeList.forEach((item,ind)=>{
        if( item.type == type){
          this.form.typeName = item.caption
        }
      })
    },
    fetchTypeList(){
      getTypeList().then(page => {
        this.typeList = page.data;
      })
    },
    //修改的时候初始化使用
    getForm(){
      getForm(this.id).then((form) =>{
        this.form = form;
        this.type = this.form.type;
      }).catch((e)=>{
        console.log("失败的情况",e)
      });




    },
    save(){
      let promise  =  null;

      // delete  this.form.createtime;

      if(this.id) {
        promise = update( this.form);
      }else{
        promise = save( this.form);
      }
      // form /
      promise.then((success) =>{
        if(success){
          this.$message.success("保存成功");

          //关掉自己
          this.$emit("update:visible",false);
          this.$emit("close");
        }
      }).catch((e)=>{
        console.log("失败的情况",e)
      });

    }
  }
}


</script>

<style scoped>




</style>
